﻿//using NCalc;
//using Newtonsoft.Json;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//public  class ScriptDemo
//{
   
//    public static Dictionary<string, Dictionary<string, int>> finalResult = new Dictionary<string, Dictionary<string, int>>();

//    public static Dictionary<string, int> toAdjustLevel = new Dictionary<string, int>();

//  //       string str1 = @"{""effortLevel"": {""standard"": ""50|80"", ""result"": ""1|2|3"", ""formula"": ""(thinking*1+thinkingActivity*1+lowFlow*1+singleMindedness*1+lfn*0.15)*100/16+max(0,(thinking-5)*10)+max(0,(lowFlow-5)*10)+max(0,(singleMindedness-5)*10)""}, ""focusOnInvestment"": {""standard"": ""50|80"", ""result"": ""1|2|3"", ""formula"": ""(lowFlow*2.1+singleMindedness*2.3+midFlow*5+highFlow*8)*100/8+max(0,(singleMindedness-5)*10)""}, ""enterprising"": {""standard"": ""50|80"", ""result"": ""1|2|3"", ""formula"": ""(impatient*3.5+worryTrait*5+crisisSense*0.8+thinkingActivity*0.8)*100/10+max(0,(impatient-5)*10)+max(0,(worryTrait-5)*10)""}, ""emotionManager"": {""standard"": ""30|60"", ""result"": ""1|2|3"", ""formula"": ""100-(emotionalActivity*0.8+emotionalStress*1+anxietyTrait*1+delicateSensitive*1+midMoodWave*0.8+highMoodWave*0.8+innerHelplessness*1+innerResistance*1)*100/6+max(0,(emotionalStress-5)*10)+max(0,(anxietyTrait-5)*10)+max(0,(innerHelplessness-5)*5)+max(0,(innerResistance-5)*8)""}, ""easyObedient"": {""standard"": ""30|60"", ""result"": ""1|2|3"", ""formula"": ""100-(worry*1+worryTrait*1+delicateSensitive*1+midMoodWave*0.8+highMoodWave*0.8+innerHelplessness*1+innerResistance*1)*100/6+max(0,(worry-5)*20)+max(0,(worryTrait-5)*10)+max(0,(delicateSensitive-5)*10)""}, ""activeMind"": {""standard"": ""50|80"", ""result"": ""1|2|3"", ""formula"": ""(thinkingActivity*0.5+thinking*1.4+lowFlow*1.3+singleMindedness*1.8+mentalStress*4)*100/5+max(0,(thinking-5)*5)+max(0,(lowFlow-5)*8)+max(0,(singleMindedness-5)*10)+max(0,(mentalStress-5)*10)""}, ""fatigueStatus"": {""standard"": ""17|40|85|95"", ""result"": ""1|2|3|4|5"", ""formula"": ""(mentalFatigue*6+visualFatigue*6+lowDepress*6+midDepress*8+highDepress*10-lfn*0.1)*100/28+max(0,(mentalFatigue-5)*10)+max(0,(lowDepress-5)*10)+max(0,(midDepress-5)*10)+max(0,(highDepress-5)*10)""}, ""energyStatus"": {""standard"": ""50|80"", ""result"": ""1|2|3"", ""formula"": ""(lfn*0.2-mentalFatigue*2.5-visualFatigue*1+thinkingActivity*1.8+crisisSense*1.8+lowAnxiety*0.8+midAnxiety*1+ highAnxiety*1-lowDepress*1.2-midDepress*1.2-highDepress*1.5)*100/30 + max(0,(thinkingActivity-5)*3)+max(0,(crisisSense-5)*5)+max(0,(highAnxiety-5)*5) - max(0,(lowDepress-5)*10)""}, ""model_codes"": [""effortLevel"", ""focusOnInvestment"", ""enterprising"", ""emotionManager"", ""easyObedient"", ""activeMind"", ""fatigueStatus"", ""energyStatus""]}";
//    //    string str2 = @"{ ""lfnSet"": [60,60,60,60,60,60],""thinkingActivitySet"": [1,1,-1,-1,-1,-1],""thinkingSet"": [1,-1,-1,-1,-1,-1],""singleMindednessSet"": [1,1,-1,-1,1,-1],""highFlowSet"": [1,-1,-1,-1,-1,-1],""midFlowSet"": [1,-1,-1,-1,-1,-1],""lowFlowSet"": [1,-1,-1,-1,-1,-1],""highAnxietySet"": [1,1,-1,-1,1,-1],""midAnxietySet"": [1,1,-1,-1,1,-1],""lowAnxietySet"": [1,1,-1,-1,1,-1],""highDepressSet"": [1,1,-1,-1,1,-1],""midDepressSet"": [1,1,-1,-1,1,-1],""lowDepressSet"": [1,1,-1,-1,1,-1],""highMoodWaveSet"": [-1,-1,-1,-1,-1,-1],""midMoodWaveSet"": [-1,-1,-1,-1,-1,-1],""innerResistanceSet"": [-1,-1,-1,-1,-1,-1],""innerHelplessnessSet"": [-1,-1,-1,-1,-1,-1],""emotionalActivitySet"": [1,-1,-1,-1,-1,-1],""emotionalStressSet"": [-1,-1,1,-1,-1,-1],""mentalFatigueSet"": [-1,1,1,-1,1,-1],""visualFatigueSet"": [-1,1,1,-1,1,-1],""mentalStressSet"": [-1,-1,-1,-1,-1,-1],""anxietyTraitSet"": [-1,-1,-1,1,-1,-1],""delicateSensitiveSet"": [-1,-1,-1,-1,-1,-1],""impatientSet"": [-1,-1,-1,-1,-1,-1],""crisisSenseSet"": [-1,-1,-1,-1,-1,-1],""worrySet"": [1,1,0,1,0,1],""worryTraitSet"": [-1,-1,-1,-1,-1,-1],""drowsinessLevel"":[0],""sleepStage"":[1]}";
//    //    string str3 = @"{""drowsinessLevel"":0,""sleepStage"":1}";


  

//    public static bool correct_condition(double drowsinessLevel, double sleepStage)
//    {
//        if (drowsinessLevel > 0 || sleepStage == 1 || sleepStage == 2 || sleepStage == 3 || sleepStage == 0)
//        {
//            return true;
//        }
//        return false;
//    }

//    public static Dictionary<string, double> parametersResult = new Dictionary<string, double>();

//    public static bool ConfigureParas(string neededIndexes,string setting_data)
//    {


        
//    }


//    public static string OneMinuteAnalysis(string setting_data, string value_json, string ext_value)
//    {

//        Dictionary<string, object> settingData = JsonConvert.DeserializeObject<Dictionary<string, object>>(setting_data);

//        Dictionary<string, CRule> neededRule = new Dictionary<string, CRule>();

//        parametersResult = new Dictionary<string, double>();

//        //ready to convert to JsonString 
//        Dictionary<string, Result> final_result = new Dictionary<string, Result>();


//        List<string> model_codes = new List<string>();


//        Dictionary<string, object> variables = new Dictionary<string, object>();

       
//        model_codes = JsonConvert.DeserializeObject<List<String>>(settingData["model_codes"].ToString());



//        foreach (string item in model_codes)
//        {
//            // neededRule.Add(item, settingData[item]);
//            CRule a = JsonConvert.DeserializeObject<CRule>(settingData[item].ToString());
//            settingData[item] = a;
//            neededRule.Add(item, a);
//            //      Console.WriteLine(a.formula);

//            final_result.Add(item, new Result() { });

//            toAdjustLevel.Add(item, 0);

//        }



//        // 解析  一分钟对应的数据   


//        //  Dictionary<string, List<int>> settingDataValue = JsonConvert.DeserializeObject<Dictionary<string, List<int>>(value_json);

//        parametersResult.Clear();

//        Dictionary<string, List<int>> settingDataValue = JsonConvert.DeserializeObject<Dictionary<string, List<int>>>(value_json);

//        string totalStr = settingDataValue.ToString();
//        if (totalStr.Contains("-99"))
//        {
//            foreach (var item in settingDataValue.Keys)
//            {
//                finalResult[item] = new Dictionary<string, int>()
//                      {
//                    {"score", -1},
//                    {"level",-1}
//                       };
//            }
//            return finalResult.ToString();
//        }
//        foreach (var item in settingDataValue.Keys)
//        {

//            if (item == "lfnSet")
//            {
//                var var1 = settingDataValue[item].Sum(x => x);
//                var var2 = settingDataValue[item].Count(x => x == -1);
//                var var3 = settingDataValue[item].Count;
//                var var4 = (var1 + var2) / (var3 - var2);
//                parametersResult.Add("lfn", var4);
//            }
//            else if (item == "drowsinessLevel" || item == "sleepStage")
//            {
//                parametersResult.Add(item, settingDataValue[item][settingDataValue[item].Count - 1]);
//            }
//            else
//            {
//                string var5 = item.Replace("Set", "");
//                parametersResult.Add(var5, settingDataValue[item].Count(x => x == 1));
//            }
//            // parametersResult.Add(item,)

//        }

//        foreach (string itemX in neededRule.Keys)
//        {
//            CalculateCfactor(itemX, neededRule);
//        }

//        string json = JsonConvert.SerializeObject(finalResult);


//        return json;
//    }
//    public static List<string> FindItemsWithChar(List<string> lst, string str)
//    {
//        List<string> result = new List<string>();
//        foreach (string item in lst)
//        {
//            if (item.Contains(str))
//            {
//                result.Add(item);
//            }
//        }
//        return result;
//    }


//    public static double EvaluateFormula(string someIndex, string formula)
//    {
//        // Evaluate the formula and return the score

//        Expression e = new Expression(formula, EvaluateOptions.IgnoreCase);

//        foreach (string item in parametersResult.Keys)
//        {
//            e.Parameters[item] = parametersResult[item];
//        }

//        var result = e.Evaluate();


//        return (double)result;
//    }


//    public static int AdjustLevel(string formula)
//    {
//        // Evaluate the formula and return the score

//        Expression e = new Expression(formula, EvaluateOptions.IgnoreCase);

//        foreach (string item in toAdjustLevel.Keys)
//        {
//            e.Parameters[item] = toAdjustLevel[item];
//        }

//        var result = e.Evaluate();


//        return (int)result;
//    }

//    public static void CalculateCfactor(string variable, Dictionary<string, CRule> modelSettingData)
//    {
//        List<string> correct_result = new List<string>()
//                 {
//                "Max(energyStatus - 1, 1)",
//                "Max(effortLevel - 1, 1)",
//                "Max(focusOnInvestment - 1, 1)",
//                "Max(enterprising - 1, 1)",
//                "Min(easyObedient + 1, 3)",
//                "Min(emotionManager + 1, 3)",
//                "Min(fatigueStatus + 2, 5)"
//                 };
//        double score = EvaluateFormula(variable, modelSettingData[variable].formula);

//        score = (double)ModifyScore(score);

//        string[] standard = modelSettingData[variable].standard.Split('|');
//        string[] result = modelSettingData[variable].result.Split('|');
//        int resultLevel = 0;
//        for (int i = 0; i < standard.Length; i++)
//        {
//            if (score <= double.Parse(standard[i]))
//            {
//                resultLevel = int.Parse(result[i]);
//                break;
//            }
//        }
//        if (resultLevel == 0)
//        {
//            resultLevel = int.Parse(result[result.Length - 1]);
//        }

//        toAdjustLevel[variable] = resultLevel;
//        //

//        if (correct_condition(parametersResult["drowsinessLevel"], parametersResult["sleepStage"]))
//        {
//            List<string> result2 = FindItemsWithChar(correct_result, variable);
//            if (result2.Count > 0)
//            {
//                //   resultLevel=(int)  EvaluateFormula("", result2[0]);

//                resultLevel = AdjustLevel(result2[0]);
//            }
//        }

//        finalResult[variable] = new Dictionary<string, int>()
//              {
//            {"score", (int)score},
//            {"level", resultLevel}
//               };

//    }

//    public static decimal ModifyScore(double score)
//    {
//        if (score < 0)
//        {
//            score = 20 - Math.Log(Math.Abs(score) + 1);
//        }
//        else if (score >= 99)
//        {
//            score = 90 + Math.Log(score - 99 + 1);
//        }
//        decimal score_f = Math.Round((decimal)score, 0);
//        return score_f;
//    }
//}
//public class CRule
//{
//    public string standard { get; set; }
//    public string result { get; set; }
//    public string formula { get; set; }
//}
//public class Result
//{
//    public int score { get; set; }
//    public int level { get; set; }
//}